package cn.xuchunh.campusbbs.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.text.util.Linkify;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.bean.ReplyContent;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 * Created by XuChunH on 2016/1/11.
 */
public class ReplyContentView extends LinearLayout {

    private int imageHeight;

    private OnContentClickListener listener;

    public ReplyContentView(Context context) {
        this(context, null);
    }

    public ReplyContentView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ReplyContentView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        imageHeight = getResources().getDimensionPixelSize(R.dimen.reply_content_image_height);
    }

    public void setContent(List<ReplyContent> contentList) {
        if (contentList != null && contentList.size() > 0) {
            for (ReplyContent content : contentList) {
                switch (content.getType()) {
                    case ReplyContent.TYPE_TEXT:
                        addText(content);
                        break;
                    case ReplyContent.TYPE_IMAGE:
                        addImage(content);
                        break;
                    case ReplyContent.TYPE_VIDEO:
                        addVideo(content);
                        break;
                    case ReplyContent.TYPE_INTERNAL_LINK:
                    case ReplyContent.TYPE_EXTERNAL_LINK:
                        addLink(content);
                        break;
                    default:
                        break;
                }
            }
        }
    }

    private void addText(ReplyContent content){
        AdvancedTextView textView =  new AdvancedTextView(getContext());
        textView.setRichText(content.getInfo());
        addView(textView);
    }

    private void addImage(ReplyContent content){
        SimpleDraweeView draweeView = new SimpleDraweeView(getContext());
        GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources());
        builder.setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER)
                .setPlaceholderImage(new ColorDrawable(getResources().getColor(R.color.placeholderImage)), ScalingUtils.ScaleType.FIT_CENTER)
                .setFailureImage(new ColorDrawable(getResources().getColor(R.color.placeholderImage)), ScalingUtils.ScaleType.FIT_CENTER);
        draweeView.setHierarchy(builder.build());
        draweeView.setImageURI(Uri.parse(content.getOriginalInfo()));
        LayoutParams layoutParams = new LayoutParams(MATCH_PARENT, imageHeight);
        draweeView.setLayoutParams(layoutParams);
        draweeView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onImageClick(ReplyContentView.this, v);
                }
            }
        });
        addView(draweeView);
    }

    private void addVideo(ReplyContent content){

    }

    private void addLink(ReplyContent content){
        TextView textView = new TextView(getContext());
        textView.setAutoLinkMask(Linkify.WEB_URLS);
        textView.setText(content.getInfo());
        addView(textView);
    }

    public OnContentClickListener getListener() {
        return listener;
    }

    public void setListener(OnContentClickListener listener) {
        this.listener = listener;
    }

    interface OnContentClickListener{
        void onImageClick(ReplyContentView parent, View view);
        void onLinkClick(ReplyContentView parent, View view);
    }


}
